//
//  AudioPlayerViewController.m
//  AVFoundation
//
//  Created by bao on 2018/6/21.
//  Copyright © 2018年 bao. All rights reserved.
//

#import "AudioPlayerViewController.h"

#import <AVFoundation/AVFoundation.h>

@interface AudioPlayerViewController () <AVAudioPlayerDelegate>

@property (nonatomic, strong) AVAudioPlayer *audioPlayer;
@property (weak, nonatomic) IBOutlet UIButton *playButton;

@end

@implementation AudioPlayerViewController

- (IBAction)play {
    [self.audioPlayer play];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.playButton.enabled = NO;
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"shijianzhuyu" withExtension:@"mp3"];
    NSError *error = nil;
    AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileUrl error:&error];
    audioPlayer.delegate = self;
    audioPlayer.numberOfLoops = 2;
    if (error) {
        DCLog(@"error : %@", error.localizedDescription);
    } else {
        [audioPlayer prepareToPlay];
        self.audioPlayer = audioPlayer;
        self.playButton.enabled = YES;
        [audioPlayer playAtTime:60];
    }
}

#pragma mark - AVAudioPlayerDelegate

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    DCLog(@"audioPlayerDidFinishPlaying---%d", flag);
}

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
{
    DCLog(@"audioPlayerDecodeErrorDidOccur---%@", error.localizedDescription);
}

@end
